﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP.Lesson
{
    public partial class add_Comment : System.Web.UI.Page
    {
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            string content = Request.QueryString["content"];

            new TblLessonCommentController().Insert(int.Parse(Session["user_id"].ToString()), content, false, false, DateTime.Now);
            var get_last = new TblLessonCommentController().FetchAll().Last();
            id = get_last.Id;
        }

        public string datetimeToString(string d)
        {
            string s = "";

            DateTime date = DateTime.Parse(d);
            if (DateTime.Now.Date == date.Date)
            {
                s = "Hôm nay lúc " + configTime(date.Hour) + ":" + configTime(date.Minute);
            }
            else
            {
                if (date.Year == DateTime.Now.Year && date.Month == DateTime.Now.Month && DateTime.Now.Day - date.Day == 1)
                {
                    s = "Hôm qua lúc " + configTime(date.Hour) + ":" + configTime(date.Minute);
                }
                else
                {
                    if (DateTime.Now.Year == date.Year)
                    {
                        s = date.Day + " tháng " + date.Month + " lúc " + configTime(date.Hour) + ":" + configTime(date.Minute);
                    }
                    else
                        s = date.Day + " tháng " + date.Month + " " + date.Year + " lúc " + configTime(date.Hour) + ":" + configTime(date.Minute);
                }
            }
            return s;
        }
        private string configTime(int t)
        {
            string repair = "";
            if (t < 10)
                repair = "0" + t;
            else
                repair = t.ToString();
            return repair;
        }
        public int? findReplyTeacher(int cmt)
        {
            int? id = null;
            var get_reply = new TblLessonCommentReplyController().FetchAll().Where(x => x.CmtId == cmt);
            foreach (TblLessonCommentReply reply in get_reply)
            {
                var get_teacher_reply = new TblUserController().FetchAll().Where(x => x.UserId == reply.UserId).Single();
                if (get_teacher_reply.UserLevel == 5)
                    id = reply.Id;
            }
            return id;
        }
    }
}